package com.bw.gmall.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.common.base.BaseApp;
import com.bw.gmall.realtime.common.bean.UserLoginBean;
import com.bw.gmall.realtime.common.constant.Constant;
import com.bw.gmall.realtime.common.function.DorisMapFunction;
import com.bw.gmall.realtime.common.util.DateFormatUtil;
import com.bw.gmall.realtime.common.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Iterator;

/**
 * @Author sw
 * @CreateTime 2024-11-20
 * 12.4 用户域用户登录各窗口汇总表
 */
public class DwsUserUserLoginWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsUserUserLoginWindow().start(10024,4,
                Constant.DWS_USER_USER_LOGIN_WINDOW,
                Constant.TOPIC_DWD_TRAFFIC_PAGE);
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> dataStreamSource) {
        //过滤出所有登录记录
        SingleOutputStreamOperator<JSONObject> filterStream = getFilterStream(dataStreamSource);
        //解析
        //回流用户：判断今天和最后一次登录日期的差是否大于7
        SingleOutputStreamOperator<UserLoginBean> beanStream = parseToPoJo(filterStream);
        //开窗聚合
        SingleOutputStreamOperator<UserLoginBean> resultStream = getResultStream(beanStream);
        //写到Doris
        resultStream.map(new DorisMapFunction<>())
                .sinkTo(FlinkSinkUtil.getDorisSink("dws_user_user_login_window"));
    }

    private static SingleOutputStreamOperator<UserLoginBean> getResultStream(SingleOutputStreamOperator<UserLoginBean> beanStream) {
         return beanStream.assignTimestampsAndWatermarks(WatermarkStrategy
                        .<UserLoginBean>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                        .withTimestampAssigner((event, timestamp) -> event.getTs())
                        .withIdleness(Duration.ofSeconds(120)))
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<UserLoginBean>() {
                            @Override
                            public UserLoginBean reduce(UserLoginBean x, UserLoginBean y) throws Exception {
                                x.setUuCt(x.getUuCt() + y.getUuCt());
                                y.setBackCt(x.getBackCt() + y.getBackCt());
                                return x;
                            }
                        },
                        new AllWindowFunction<UserLoginBean, UserLoginBean, TimeWindow>() {
                            @Override
                            public void apply(TimeWindow timeWindow, Iterable<UserLoginBean> iterable, Collector<UserLoginBean> collector) throws Exception {
                                Iterator<UserLoginBean> iterator = iterable.iterator();
                                UserLoginBean bean = iterator.next();
                                bean.setStt(DateFormatUtil.tsToDateTime(timeWindow.getStart()));
                                bean.setEdt(DateFormatUtil.tsToDateTime(timeWindow.getEnd()));
                                bean.setCurDate(DateFormatUtil.tsToDate(System.currentTimeMillis()));
                                collector.collect(bean);
                            }
                        });
    }

    private static SingleOutputStreamOperator<UserLoginBean> parseToPoJo(SingleOutputStreamOperator<JSONObject> filterStream) {
        return filterStream.keyBy(x -> x.getJSONObject("common").getString("uid"))
                .process(new KeyedProcessFunction<String, JSONObject, UserLoginBean>() {
                    ValueState<String> lastLoginState;

                    @Override
                    public void open(Configuration parameters) {
                        lastLoginState = getRuntimeContext().getState(
                                new ValueStateDescriptor<>("lastLoginState", String.class));
                    }

                    @Override
                    public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, UserLoginBean>.Context context, Collector<UserLoginBean> collector) throws Exception {
                        Long ts = jsonObject.getLong("ts");
                        String today = DateFormatUtil.tsToDate(ts);
                        String lastLoginValue = lastLoginState.value();
                        Long uuCt = 0L;
                        Long backCt = 0L;
                        if (!today.equals(lastLoginValue)) {
                            //今天的第一次登录
                            uuCt = 1L;
                            lastLoginState.update(today);
                            //计算回流：曾经登陆过
                            if (lastLoginValue != null) {
                                long lastLoginTs = DateFormatUtil.dateToTs(lastLoginValue);
                                //7日回流
                                if ((ts - lastLoginTs) / 1000 / 60 / 60 / 24 > 7) {
                                    backCt = 1L;
                                }
                            }
                        }
                        if (uuCt == 1) {
                            collector.collect(new UserLoginBean("", "", "", backCt, uuCt, ts));
                        }
                    }
                });
    }

    private static SingleOutputStreamOperator<JSONObject> getFilterStream(DataStreamSource<String> dataStreamSource) {
        return dataStreamSource.map(JSON::parseObject)
                .filter(new FilterFunction<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) throws Exception {
                        //有保存登录状态的自动登录和手动登录
                        //要用用户id，可能同一用户不同设备，所以不能用mid
                        String uid = jsonObject.getJSONObject("common").getString("uid");
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        return uid != null && (lastPageId == null || "login".equals(lastPageId));
                    }
                });
    }
}
